//
//  OrderListTableViewCell.m
//  much
//
//  Created by Believer Just on 2019/1/11.
//  Copyright © 2019年 iOS. All rights reserved.
//

#import "OrderListTableViewCell.h"

@interface OrderListTableViewCell ()

/** 商品图片 */
@property (strong, nonatomic) UIImageView *goodsImageView;
/** 标题 */
@property (strong, nonatomic) UILabel *goodsTitleLabel;
/** 副标题 */
@property (strong, nonatomic) UILabel *subheadLabel;
/** 商品数量 */
@property (strong, nonatomic) UILabel *countLabel;
/** 缺货标记 */
@property (strong, nonatomic) UIView *defectMarkView;
/** 商品名称前的标签 */
@property (nonatomic, strong) UILabel *lotteryLabel;
/** 当前价格 */
@property (nonatomic, strong) UILabel *nowPriceLabel;
/** 划线价 */
@property (nonatomic, strong) UILabel *beforeLabel;
/** 退款状态 */
@property (nonatomic, strong) UILabel *refoundStateLabel;
/** 查看售后按钮 */
@property (nonatomic, strong) UIButton *watchRefoundButton;
/** 退款按钮 */
@property (nonatomic, strong) UIButton *refoundStateButton;
/** 退款透明按钮 */
@property (nonatomic, strong) UIButton *clearRefoundButton;
/** 商品标题标签 */
@property (nonatomic, strong) UILabel *markLabel;

@end

@implementation OrderListTableViewCell

+ (instancetype)initTableView:(UITableView *)tableView {
    OrderListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([OrderListTableViewCell class])];
    if (!cell) {
        cell = [[OrderListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([OrderListTableViewCell class])];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
    }
    return self;
}

- (void)setModel:(GoodsModel *)model {
    _model = model;
    
    /**
     [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:PacketImageUrl(model.imagePath)] placeholderImage:NewStoreDefault];
     //商品名称前添加标签
     if (model.benefit == JoinHuiyuanMallType) {
     // 商品标题
     [self updateTitleContent:@" 积分商城 "];
     } else {
     if (model.goodsClassify == GoodsClassifyTypeSubscribe) {
     // 商品标题
     [self updateTitleContent:@" 征订 "];
     } else {
     // 商品标题
     self.lotteryLabel.hidden = YES;
     self.goodsTitleLabel.text = model.title;
     [UILabel changeLineSpaceForLabel:self.goodsTitleLabel WithSpace:1];
     }
     }
     
     self.subheadLabel.text = model.name;
     NSString * str = [NSString stringWithFormat:@"X%@", @(model.number)];
     NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:str];
     NSRange strRange = {0, 1};
     [attributedString addAttribute:NSFontAttributeName value:APPFONT(11) range:strRange];
     strRange = NSMakeRange(1, attributedString.length - 1);
     [attributedString addAttribute:NSFontAttributeName value:APPFONT(15) range:strRange];
     self.countLabel.attributedText = attributedString;
     
     self.nowPriceLabel.text = [NSString stringWithFormat:@"¥%@", _model.unitPrice];
     if (_model.regularPrice.hasValue && ([_model.regularPrice floatValue] > [_model.sellingPrice floatValue])) {
     self.beforeLabel.hidden = NO;
     self.beforeLabel.text = [NSString stringWithFormat:@"¥%@", _model.regularPrice];
     } else {
     self.beforeLabel.hidden = YES;
     }
     
     if (_model.groupBuying) {
     [self updateTitleContent:@" [拼团] "];
     }
     else if (_model.inLimitBuy){
     //限时购增加
     [self updateTitleContent:@" [限时购] "];
     }
     
     if (self.beforeLabel.text.hasValue) {
     NSMutableAttributedString *newPrice = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", self.beforeLabel.text]];
     [newPrice addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle) range:NSMakeRange(0, newPrice.length)];
     self.beforeLabel.attributedText = newPrice;
     }
     
     self.refoundStateButton.hidden = NO;
     if (_model.drawbackEnable) {
     if (self.orderModel.orderStateIndex == OrderStateSuccess) {
     if (_model.couponType.hasValue) {
     if ([_model.couponType isEqualToString:@"goods_coupon"] || [_model.couponType isEqualToString:@"cash_coupon"]) {
     [self.refoundStateButton setTitle:@"申请换货" forState:UIControlStateNormal];
     }
     } else {
     [self.refoundStateButton setTitle:@"申请售后" forState:UIControlStateNormal];
     }
     } else {
     [self.refoundStateButton setTitle:@"申请售后" forState:UIControlStateNormal];
     }
     
     if (_model.afterSalesdrawbackEnable) {
     self.watchRefoundButton.hidden = NO;
     } else {
     self.watchRefoundButton.hidden = YES;
     }
     
     self.refoundStateButton.userInteractionEnabled = YES;
     [self.refoundStateButton setTitleColor:Color34 forState:UIControlStateNormal];
     [self.refoundStateButton roundBorderWithBorderDirection:BorderDirectionALL lineWidth:0.5 color:Color140];
     } else {
     if (_model.afterSalesdrawbackEnable) {
     self.watchRefoundButton.hidden = NO;
     } else {
     self.refoundStateButton.hidden = YES;
     self.watchRefoundButton.hidden = YES;
     }
     
     self.refoundStateButton.userInteractionEnabled = NO;
     [self.refoundStateButton setTitleColor:RGBACOLOR(196, 196, 196, 1) forState:UIControlStateNormal];
     [self.refoundStateButton roundBorderWithBorderDirection:BorderDirectionALL lineWidth:0.5 color:RGBACOLOR(218, 218, 218, 1)];
     
     }
     
     self.clearRefoundButton.hidden = self.refoundStateButton.hidden;
     self.clearRefoundButton.userInteractionEnabled = self.refoundStateButton.userInteractionEnabled;
     
     if (model.overseas && model.title.hasValue) {
     self.markLabel.hidden = NO;
     NSMutableParagraphStyle *muParagraph = [[NSMutableParagraphStyle alloc]init];
     muParagraph.lineSpacing = 2;
     muParagraph.firstLineHeadIndent = 35;
     
     NSString *title = model.title;
     if (model.groupBuying) {
     self.lotteryLabel.hidden = YES;
     title = [NSString stringWithFormat:@" [拼团] %@",title];
     }
     else if (model.inLimitBuy) {
     // 限时购
     self.lotteryLabel.hidden = YES;
     title = [NSString stringWithFormat:@" [限时购] %@",title];
     }
     
     NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:title attributes:@{NSParagraphStyleAttributeName : muParagraph}];
     if ([title hasPrefix:@" [拼团] "]) {
     [attr addAttributes:@{NSForegroundColorAttributeName : Color175030} range:NSMakeRange(0, @" [拼团] ".length)];
     }else if ([title hasPrefix:@" [限时购] "]){
     [attr addAttributes:@{NSForegroundColorAttributeName : Color175030} range:NSMakeRange(0, @" [限时购] ".length)];
     }
     self.goodsTitleLabel.attributedText = attr;
     } else {
     self.markLabel.hidden = YES;
     }
     */
}

/** 更新商品标题的内容 */
- (void)updateTitleContent:(NSString *)lotteryString {
    // 通过需要添加的标签的大小，来判断内容的缩进
    CGSize size = TextSize(lotteryString, APPFONT(14));
    self.lotteryLabel.hidden = NO;
    self.lotteryLabel.text = lotteryString;
    if ([lotteryString containsString:@"拼团"] ||
        [lotteryString containsString:@"限时购"]) {
        _lotteryLabel.layer.masksToBounds = NO;
        _lotteryLabel.layer.cornerRadius = 0;
        _lotteryLabel.layer.borderColor = [[UIColor clearColor] CGColor];
        _lotteryLabel.layer.borderWidth = 0;
    } else {
        _lotteryLabel.layer.masksToBounds = YES;
        _lotteryLabel.layer.cornerRadius = 2;
        _lotteryLabel.layer.borderColor = [Color175030 CGColor];
        _lotteryLabel.layer.borderWidth = 1;
    }
    
    /**
     NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:self.model.title];
     
     NSMutableParagraphStyle * style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
     style.firstLineHeadIndent = size.width + 5; // 首行头缩进
     [style setLineSpacing:1];
     [attrString addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [self.model.title length])];
     self.goodsTitleLabel.attributedText = attrString;
     */
}

- (void)progressRefoundStateButton {
    if (self.orderListCellRefoundButtonBlock) {
        self.orderListCellRefoundButtonBlock(self.model, self.orderModel);
    }
}
- (void)progressWatchRefoundButton {
    if (self.orderListCellWatchRefoundButtonBlock) {
        self.orderListCellWatchRefoundButtonBlock(self.model, self.orderModel);
    }
}


#pragma mark - 懒加载
- (UIImageView *)goodsImageView {
    if (!_goodsImageView) {
        _goodsImageView = [UIImageView new];
        _goodsImageView.contentMode = UIViewContentModeScaleAspectFit;
        _goodsImageView.cornerRadius = 2;
        _goodsImageView.layer.borderColor = RGBACOLOR(213, 213, 213, 1).CGColor;
        _goodsImageView.layer.borderWidth = 0.5;
        [self.contentView addSubview:_goodsImageView];
        [_goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).with.offset(18);
            make.top.equalTo(self.contentView).with.offset(7);
            make.bottom.equalTo(self.contentView).with.offset(- 7);
            make.width.equalTo(self.goodsImageView.mas_height);
        }];
        
        UIView * line = [UIView new];
        line.backgroundColor = HEXCOLOR(0xE4E4E4);
        [self.contentView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.goodsImageView);
            make.bottom.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _goodsImageView;
}
- (UILabel *)goodsTitleLabel {
    if (!_goodsTitleLabel) {
        _goodsTitleLabel = [UILabel new];
        _goodsTitleLabel.font = APPFONT(14);
        _goodsTitleLabel.textColor = HEXCOLOR(0x353535);
        _goodsTitleLabel.numberOfLines = 2;
        [self.contentView addSubview:_goodsTitleLabel];
        [_goodsTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.goodsImageView.mas_right).with.offset(10);
            make.top.equalTo(self.goodsImageView);
            make.height.mas_lessThanOrEqualTo(@42);
            make.width.mas_equalTo(176.0 / 375.0 * ScreenWidth);
        }];
    }
    return _goodsTitleLabel;
}
- (UILabel *)subheadLabel {
    if (!_subheadLabel) {
        _subheadLabel = [UILabel new];
        _subheadLabel.font = APPFONT(11);
        _subheadLabel.numberOfLines = 1;
        _subheadLabel.textColor = HEXCOLOR(0x8a8a8a);
        [self.contentView addSubview:_subheadLabel];
        [_subheadLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.goodsTitleLabel);
            make.top.equalTo(self.goodsTitleLabel.mas_bottom).with.offset(4);
            make.right.equalTo(self.goodsTitleLabel);
        }];
    }
    return _subheadLabel;
}
- (UILabel *)countLabel {
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.font = APPFONT(12);
        _countLabel.textColor = Color140;
        _countLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_countLabel];
        [_countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.goodsTitleLabel);
            make.bottom.equalTo(self.goodsImageView);
            make.width.mas_lessThanOrEqualTo(@40);
        }];
    }
    return _countLabel;
}
- (UIView *)defectMarkView {
    if (!_defectMarkView) {
        _defectMarkView = [UIView new];
        _defectMarkView.hidden = YES;
        _defectMarkView.backgroundColor = [UIColor whiteColor];
        [self.goodsImageView addSubview:_defectMarkView];
        [_defectMarkView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(34);
            make.centerY.equalTo(self.goodsImageView);
            make.height.mas_equalTo(22);
        }];
        
        UILabel * defectMarkLabel = [UILabel new];
        defectMarkLabel.cornerRadius = 2;
        defectMarkLabel.text = @"已下架";
        defectMarkLabel.font = APPFONT(9);
        defectMarkLabel.textColor = [UIColor whiteColor];
        defectMarkLabel.backgroundColor = HEXCOLORALPHA(000000, 0.7);
        defectMarkLabel.textAlignment = NSTextAlignmentCenter;
        [_defectMarkView addSubview:defectMarkLabel];
        [defectMarkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.defectMarkView);
        }];
    }
    return _defectMarkView;
}
- (UILabel *)lotteryLabel {
    if (!_lotteryLabel) {
        _lotteryLabel = [[UILabel alloc] init];
        _lotteryLabel.font = APPFONT(14);
        _lotteryLabel.textColor = Color175030;
        _lotteryLabel.layer.masksToBounds = YES;
        _lotteryLabel.layer.cornerRadius = 2;
        _lotteryLabel.layer.borderColor = [Color175030 CGColor];
        _lotteryLabel.layer.borderWidth = 1;
        [self.contentView addSubview:_lotteryLabel];
        [_lotteryLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(self.goodsTitleLabel);
        }];
    }
    return _lotteryLabel;
}
- (UILabel *)nowPriceLabel {
    if (!_nowPriceLabel) {
        _nowPriceLabel = [UILabel new];
        _nowPriceLabel.textColor = Color34;
        _nowPriceLabel.textAlignment = NSTextAlignmentRight;
        _nowPriceLabel.font = APPFONT(14);
        [self.contentView addSubview:_nowPriceLabel];
        [_nowPriceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).with.offset(- 15);
            make.top.equalTo(self.goodsImageView);
            make.width.mas_equalTo(70 / 375.0 * ScreenWidth);
        }];
    }
    return _nowPriceLabel;
}
- (UILabel *)beforeLabel {
    if (!_beforeLabel) {
        _beforeLabel = [UILabel new];
        _beforeLabel.textColor = Color140;
        _beforeLabel.textAlignment = NSTextAlignmentRight;
        _beforeLabel.font = APPFONT(14);
        [self.contentView addSubview:_beforeLabel];
        [_beforeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.nowPriceLabel);
            make.top.equalTo(self.nowPriceLabel.mas_bottom);
            make.width.equalTo(self.nowPriceLabel);
        }];
    }
    return _beforeLabel;
}
- (UILabel *)refoundStateLabel {
    if (!_refoundStateLabel) {
        _refoundStateLabel = [UILabel new];
        _refoundStateLabel.textColor = RGBACOLOR(245, 166, 35, 1);
        _refoundStateLabel.textAlignment = NSTextAlignmentRight;
        _refoundStateLabel.font = APPFONT(12);
        [self.contentView addSubview:_refoundStateLabel];
        [_refoundStateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.refoundStateButton.mas_left).with.offset(- 10);
            make.centerY.equalTo(self.refoundStateButton);
        }];
    }
    return _refoundStateLabel;
}
- (UIButton *)watchRefoundButton {
    if (!_watchRefoundButton) {
        _watchRefoundButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _watchRefoundButton.titleLabel.font = APPFONT(12);
        _watchRefoundButton.cornerRadius = 13.0;
        [_watchRefoundButton setTitle:@"查看售后" forState:UIControlStateNormal];
        [_watchRefoundButton setTitleColor:Color34 forState:UIControlStateNormal];
        [_watchRefoundButton roundBorderWithBorderDirection:BorderDirectionALL lineWidth:0.5 color:Color140];
        [_watchRefoundButton addTarget:self action:@selector(progressWatchRefoundButton) forControlEvents:UIControlEventTouchUpInside];
        _watchRefoundButton.hidden = YES;
        [self.contentView addSubview:_watchRefoundButton];
        [_watchRefoundButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.refoundStateButton.mas_left).with.offset(- 15);
            make.bottom.equalTo(self.contentView).with.offset(- 12);
            make.width.mas_equalTo(@66);
            make.height.mas_equalTo(@26);
        }];
    }
    return _watchRefoundButton;
}
- (UIButton *)refoundStateButton {
    if (!_refoundStateButton) {
        _refoundStateButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _refoundStateButton.titleLabel.font = APPFONT(12);
        _refoundStateButton.cornerRadius = 13.0;
        [_refoundStateButton setTitle:@"申请售后" forState:UIControlStateNormal];
        [_refoundStateButton setTitleColor:Color34 forState:UIControlStateNormal];
        [_refoundStateButton roundBorderWithBorderDirection:BorderDirectionALL lineWidth:0.5 color:Color140];
        [_refoundStateButton addTarget:self action:@selector(progressRefoundStateButton) forControlEvents:UIControlEventTouchUpInside];
        _refoundStateButton.hidden = YES;
        [self.contentView addSubview:_refoundStateButton];
        [_refoundStateButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.beforeLabel);
            make.bottom.equalTo(self.contentView).with.offset(- 12);
            make.width.mas_equalTo(@66);
            make.height.mas_equalTo(@26);
        }];
    }
    return _refoundStateButton;
}
- (UIButton *)clearRefoundButton {
    if (!_clearRefoundButton) {
        _clearRefoundButton = [UIButton buttonWithType:UIButtonTypeSystem];
        _clearRefoundButton.backgroundColor = [UIColor clearColor];
        [_clearRefoundButton addTarget:self action:@selector(progressRefoundStateButton) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_clearRefoundButton];
        [_clearRefoundButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.refoundStateButton);
            make.left.equalTo(self.refoundStateButton);
            make.right.equalTo(self.contentView);
            make.bottom.equalTo(self.contentView);
        }];
    }
    return _clearRefoundButton;
}
/** 商品标题标签文字 */
- (UILabel *)markLabel {
    if (!_markLabel) {
        _markLabel = [[UILabel alloc] init];
        _markLabel.text = @"跨境";
        _markLabel.font = APPFONT(12);
        _markLabel.textAlignment = NSTextAlignmentCenter;
        _markLabel.backgroundColor = [UIColor clearColor];
        _markLabel.layer.borderWidth = 0.5;
        _markLabel.layer.cornerRadius = 1;
        _markLabel.layer.masksToBounds = YES;
        _markLabel.textColor = Color175030;
        _markLabel.layer.borderColor = Color175030.CGColor;
        [self.contentView addSubview:_markLabel];
        CGFloat titleH = [@"跨境" boundingRectWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : APPFONT(12)} context:nil].size.height;
        [_markLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.goodsTitleLabel).offset(1);
            make.left.equalTo(self.goodsImageView.mas_right).offset(10);
            make.width.mas_equalTo(33);
            make.height.mas_equalTo(titleH);
        }];
    }
    return _markLabel;
}

@end
